<!-- by 7527189 2 0 -->
<template>
  <a-row :gutter="24">
    <a-col :md="24">
      <a-card :style="cardStyle" :bordered="false">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
          <!-- 搜索区域 -->
          <a-form layout="inline" @keyup.enter.native="searchQuery">
            <a-row :gutter="24">

              <a-col :md="12" :sm="24">
                <a-form-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-select placeholder="请选择名称查询" v-model="queryParam.name" :not-found-content="fetching ? undefined : null" mode="multiple" maxTagTextLength="4" :options="options.name" @focus="handleFocusChange('name',$event) " @select="handleSelectChange() " >
                    <template v-if="fetching" #notFoundContent>
                      <a-spin size="small" />
                    </template>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="12" :sm="24">
                <a-form-item label="制造" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-select placeholder="请选择制造商查询" v-model="queryParam.mfrs" mode="multiple" maxTagTextLength="4" :options="options.mfrs" @focus="handleFocusChange('mfrs',$event) " @select="handleSelectChange() " >
                    <template v-if="fetching" #notFoundContent>
                      <a-spin size="small" />
                    </template>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="12" :sm="24">
                <a-form-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-select placeholder="请选择规格型号查询" v-model="queryParam.model" mode="multiple" maxTagTextLength="4" :options="options.model" @focus="handleFocusChange('model',$event) "  @select="handleSelectChange() " >
                    <template v-if="fetching" #notFoundContent>
                      <a-spin size="small" />
                    </template>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="12" :sm="24">
                <a-form-item label="尺寸" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-select placeholder="请选择尺寸查询" v-model="queryParam.other_field1" mode="multiple" maxTagTextLength="4" :options="options.other_field1" @focus="handleFocusChange('other_field1',$event) "  @select="handleSelectChange() " >
                    <template v-if="fetching" #notFoundContent>
                      <a-spin size="small" />
                    </template>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="12" :sm="24">
                <a-form-item label="材质" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-select placeholder="请选择材质查询" v-model="queryParam.caizhi" mode="multiple" maxTagTextLength="4" :options="options.caizhi" @focus="handleFocusChange('caizhi',$event) "   @select="handleSelectChange()" >
                    <template v-if="fetching" #notFoundContent>
                      <a-spin size="small" />
                    </template>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="12" :sm="24">
                <a-form-item label="标准" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-select placeholder="请选择标准查询" v-model="queryParam.biaozhun" mode="multiple" maxTagTextLength="4" :options="options.biaozhun" @focus="handleFocusChange('biaozhun',$event) "   @select="handleSelectChange() ">
                    <template v-if="fetching" #notFoundContent>
                      <a-spin size="small" />
                    </template>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="12" :sm="24">
                <a-form-item label="连接" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-select placeholder="请选择连接形式查询" v-model="queryParam.lianjie" mode="multiple" maxTagTextLength="4" :options="options.lianjie" @focus="handleFocusChange('lianjie',$event) " @select="handleSelectChange() ">
                    <template v-if="fetching" #notFoundContent>
                      <a-spin size="small" />
                    </template>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="12" :sm="24">
                <a-form-item label="抛光" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-select placeholder="请选择抛光要求查询" v-model="queryParam.paoguangdu" mode="multiple" maxTagTextLength="4" :options="options.paoguangdu" @focus="handleFocusChange('paoguangdu',$event) " @select="handleSelectChange() ">
                    <template v-if="fetching" #notFoundContent>
                      <a-spin size="small" />
                    </template>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="12" :sm="24">
                <a-form-item label="密封" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-select placeholder="请选择密封形式查询" v-model="queryParam.mopian" mode="multiple" maxTagTextLength="4" :options="options.mopian" @focus="handleFocusChange('mopian',$event) " @select="handleSelectChange() ">
                    <template v-if="fetching" #notFoundContent>
                      <a-spin size="small" />
                    </template>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="12" :sm="24">
                <a-form-item label="长度" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-select placeholder="请选择长度查询" v-model="queryParam.changdu" mode="multiple" maxTagTextLength="4" :options="options.changdu" @focus="handleFocusChange('changdu',$event) " @select="handleSelectChange() ">
                    <template v-if="fetching" #notFoundContent>
                      <a-spin size="small" />
                    </template>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="12" :sm="24">
                <a-form-item label="温度" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-select placeholder="请选择温度查询" v-model="queryParam.other_field2" mode="multiple" maxTagTextLength="4" :options="options.other_field2" @focus="handleFocusChange('other_field2',$event) " @select="handleSelectChange() ">
                    <template v-if="fetching" #notFoundContent>
                      <a-spin size="small" />
                    </template>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="12" :sm="24">
                <a-form-item label="压力" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-select placeholder="请选择压力查询" v-model="queryParam.other_field3" mode="multiple" maxTagTextLength="4" :options="options.other_field3" @focus="handleFocusChange('other_field3',$event) " @select="handleSelectChange() ">
                    <template v-if="fetching" #notFoundContent>
                      <a-spin size="small" />
                    </template>
                  </a-select>
                </a-form-item>
              </a-col>
              <!--              <a-col :md="4" :sm="24">-->
              <!--                <a-form-item label="技术参数" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
              <!--                  <a-input placeholder="请输入其他技术参数查询" v-model="queryParam.standard"></a-input>-->
              <!--                </a-form-item>-->
              <!--              </a-col>-->

              <!--              <template v-if="toggleSearchStatus">-->
              <a-col :md="12" :sm="24">
                <a-form-item label="类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-tree-select style="width:100%" :dropdownStyle="{maxHeight:'200px',overflow:'auto'}" allow-clear
                                 :treeData="categoryTree" v-model="queryParam.categoryId" placeholder="请选择类别">
                  </a-tree-select>
                </a-form-item>
              </a-col>
              <a-col :md="12" :sm="24">
                <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-select placeholder="请选择状态" v-model="queryParam.enabled">
                    <a-select-option value="1">启用</a-select-option>
                    <a-select-option value="0">禁用</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>

              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                <a-col :md="6" :sm="24">
                  <a-button type="primary" @click="searchQuery">查询</a-button>
                  <a-button style="margin-left: 8px" @click="searchReset">重置</a-button>
                  <!--                  <a @click="handleToggleSearch" style="margin-left: 8px">-->
                  <!--                    {{ toggleSearchStatus ? '收起' : '展开' }}-->
                  <!--                    <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>-->
                  <!--                  </a>-->
                </a-col>
              </span>
            </a-row>
          </a-form>
        </div>
        <!-- 操作按钮区域 -->
        <div class="table-operator"  style="margin-top: 5px">
          <a-button v-if="btnEnableList.indexOf(1)>-1" @click="handleAdd" type="primary" icon="plus">新增</a-button>
          <a-button v-if="btnEnableList.indexOf(1)>-1" @click="handleImportXls()" type="primary" icon="import">导入</a-button>
          <!--          <a-button type="primary" icon="download" @click="handleExportXls('商品信息')">导出</a-button>-->

          <a-popover trigger="click" placement="right">
            <template slot="content">
              <a-checkbox-group @change="onColChange" v-model="settingColumns" :defaultValue="settingColumns">
                <a-row style="width: 500px">
                  <template v-for="(item,index) in defColumns">
                    <template>
                      <a-col :span="8">
                        <a-checkbox :value="item.dataIndex">
                          <j-ellipsis :value="item.title" :length="10"></j-ellipsis>
                        </a-checkbox>
                      </a-col>
                    </template>
                  </template>
                </a-row>
              </a-checkbox-group>
            </template>
            <a-button icon="setting">列设置</a-button>
          </a-popover>
        </div>
        <!-- table区域-begin -->
        <div>
          <a-table
            ref="table"
            size="middle"
            bordered
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :components="handleDrag(columns)"
            :pagination="ipagination"
            :scroll="scroll"
            :loading="loading"
            :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange, columnWidth:'40px'}"
            @change="handleTableChange">
            <span slot="action" slot-scope="text, record">
              <a @click="viewHistory(record)">查看</a>
              <a-divider v-if="btnEnableList.indexOf(1)>-1" type="vertical" />
              <a-popconfirm v-if="btnEnableList.indexOf(1)>-1" title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                <a>删除</a>
              </a-popconfirm>
            </span>
            <template slot="customPic" slot-scope="text, record">
              <a-popover placement="right" trigger="click">
                <template slot="content">
                  <img :src="getImgUrl(record.imgName, '')" width="500px" />
                </template>
                <div style="width:52px;height:52px;" v-if="record.imgName">
                  <img v-if="record.imgName" :src="getImgUrl(record.imgName, 'mini')" class="item-img" title="查看大图" />
                </div>
              </a-popover>
            </template>
            <template slot="customBarCode" slot-scope="text, record">
              {{record.mBarCode}}
            </template>
            <template slot="customName" slot-scope="text, record">
              {{record.name}}
              <a-tag v-if="record.enableSerialNumber==1" color="orange">序</a-tag>
              <a-tag v-if="record.enableBatchNumber==1" color="orange">批</a-tag>
            </template>
            <template slot="customRenderStock" slot-scope="text, record">
              <a-tooltip :title="record.bigUnitStock">
                {{text}}
              </a-tooltip>
            </template>
            <template slot="customRenderEnabled" slot-scope="enabled">
              <a-tag v-if="enabled" color="green">启用</a-tag>
              <a-tag v-if="!enabled" color="orange">禁用</a-tag>
            </template>
          </a-table>
        </div>
        <!-- table区域-end -->
      </a-card>
    </a-col>

    <purchase-history-modal ref="purchaseHistoryModal" @ok="modalFormOk"></purchase-history-modal>

  </a-row>



</template>
<script>
import MaterialModal from '../material/modules/MaterialModal'
import PurchaseHistoryModal from '../report/modules/PurchaseHistoryModal'
import ImportFileModal from '@/components/tools/ImportFileModal'
import { materialDbSelectDynamic, queryMaterialCategoryTreeList } from '@/api/api'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import JEllipsis from '@/components/jeecg/JEllipsis'
import JDate from '@/components/jeecg/JDate'
import Vue from 'vue'

export default {
  name: "MaterialDatabase",
  mixins:[JeecgListMixin],
  components: {
    MaterialModal,
    PurchaseHistoryModal,
    ImportFileModal,
    JEllipsis,
    JDate
  },
  data () {
    return {
      categoryTree:[],
      mPropertyListShort: '',
      model: {},
      labelCol: {
        span: 5
      },
      wrapperCol: {
        span: 18,
        offset: 1
      },
      fetching:false,
      options:{
        categoryId:[],
        name:[],
        mfrs:[],
        model:[],
        other_field1:[],
        caizhi:[],
        biaozhun:[],
        lianjie:[],
        paoguangdu:[],
        mopian:[],
        changdu:[],
        other_field2:[],
        other_field3:[],
        standard:[],
        color:[]
      },
      // 查询条件
      queryParam: {
        categoryId:'',
        name:[],
        mfrs:[],
        model:[],
        other_field1:[],
        caizhi:[],
        biaozhun:[],
        lianjie:[],
        paoguangdu:[],
        mopian:[],
        changdu:[],
        other_field2:[],
        other_field3:[],
        standard:[],
        color:[]
      },
      pagination:{
        pageSizeOptions: ['10', '20', '30', '50', '100', '200']
      },
      // 实际表头
      columns:[],
      // 初始化设置的表头
      settingColumns:['name','mfrs','standard','model','color','categoryName','otherField1','caizhi','biaozhun','lianjie','paoguangdu','mopian','changdu','otherField2','otherField3','standard',
        'materialOther','unit', 'stock','fullBarCode','mBarCode',
        'purchaseDecimal','commodityDecimal','wholesaleDecimal','discountRate','taxRate','lowDecimal','enabled','enableSerialNumber','enableBatchNumber','action'],
      // 默认的列
      defColumns: [
        {
          title: '操作',
          dataIndex: 'action',
          align:"center",
          width: 100,
          scopedSlots: { customRender: 'action' },
        },
        // {title: '图片', dataIndex: 'pic', width: 65, scopedSlots: { customRender: 'customPic' }},
        {title: '编码', dataIndex: 'fullBarCode', width: 120},
        {title: '条码', dataIndex: 'mBarCode', width: 120},
        {title: '类别', dataIndex: 'categoryName', width: 100},
        {title: '名称', dataIndex: 'name', width: 150, scopedSlots: { customRender: 'customName' }},
        {title: '制造商', dataIndex: 'mfrs', width: 150},
        { title: '规格型号', dataIndex: 'model', width: 150},
        { title: '尺寸', dataIndex: 'otherField1', width: 150},
        { title: '材质', dataIndex: 'caizhi', width: 80},
        { title: '标准', dataIndex: 'biaozhun', width: 80},
        { title: '连接形式', dataIndex: 'lianjie', width: 80},
        { title: '抛光要求', dataIndex: 'paoguangdu', width: 80},
        { title: '密封形式', dataIndex: 'mopian', width: 80},
        { title: '长度', dataIndex: 'changdu', width: 80},
        { title: '温度', dataIndex: 'otherField2', width: 80},
        { title: '压力', dataIndex: 'otherField3', width: 80},
        { title: '其他技术参数', dataIndex: 'standard', width: 120},

        // {title: '型号', dataIndex: 'model', width: 120},
        // {title: '其他技术参数', dataIndex: 'standard', width: 120},
        // {title: '颜色', dataIndex: 'color', width: 70},
        // {title: '扩展信息', dataIndex: 'materialOther', width: 100},
        {title: '单位', dataIndex: 'unit', width: 50,
          customRender:function (t,r,index) {
            if (r) {
              let name = t?t:r.unitName
              if(r.sku) {
                return name + '[SKU]';
              } else {
                return name;
              }
            }
          }
        },
        // {title: '基础重量', dataIndex: 'weight', width: 80},
        // {title: '保质期', dataIndex: 'expiryNum', width: 60},
        {title: '库存', dataIndex: 'stock', width: 50,
          scopedSlots: { customRender: 'customRenderStock' }
        },
        // {title: '面价', dataIndex: 'wholesaleDecimal', width: 80},
        // {title: '采购单价', dataIndex: 'purchaseDecimal', width: 80},
        // {title: '成本价', dataIndex: 'lowDecimal', width: 80},
        // {title: '仓位货架', dataIndex: 'position', width: 80},
        {title: '备注', dataIndex: 'remark', width: 80},
        {title: '状态', dataIndex: 'enabled', align: "center", width: 60,
          scopedSlots: { customRender: 'customRenderEnabled' }
        }
      ],
      url: {
        list: "/material/database/list"
      }
    }
  },
  created() {
    this.model = Object.assign({}, {});
    this.initColumnsSetting()
    this.loadTreeData();
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}${this.url.importExcelUrl}`;
    }
  },
  methods: {
    handleSelectChange(event){
      this.searchQuery();
    },
    handleFocusChange(field,event){
      this.fetching = true;
      let params = {
        'field':field,
        'queryParam':this.queryParam,
      }
      this.options[field].splice(0,this.options[field].length)
      materialDbSelectDynamic(params).then(res => {
        res.data.options.forEach((item,index) => {
          this.$set(this.options[field], index, item);
        });
        this.fetching = false;
      });
    },
    //加载初始化列
    initColumnsSetting(){
      let columnsStr = Vue.ls.get('materialColumns')
      console.log(columnsStr)
      if(columnsStr && columnsStr.indexOf(',')>-1) {
        this.settingColumns = columnsStr.split(',')
      }
      this.columns = this.defColumns.filter(item => {
        if (this.settingColumns.includes(item.dataIndex)) {
          return true
        }
        return false
      })
      console.log(this.columns)
    },
    //列设置更改事件
    onColChange (checkedValues) {
      this.columns = this.defColumns.filter(item => {
        if (checkedValues.includes(item.dataIndex)) {
          return true
        }
        return false
      })
      let columnsStr = checkedValues.join()
      Vue.ls.set('materialColumns', columnsStr)
    },
    loadTreeData(){
      let that = this;
      let params = {};
      params.id='';
      queryMaterialCategoryTreeList(params).then((res)=>{
        if(res){
          that.categoryTree = [];
          for (let i = 0; i < res.length; i++) {
            let temp = res[i];
            that.categoryTree.push(temp);
          }
        }
      })
    },

    searchReset() {
      this.queryParam = {
        categoryId:'',
        name:[],
        mfrs:[],
        model:[],
        other_field1:[],
        caizhi:[],
        biaozhun:[],
        lianjie:[],
        paoguangdu:[],
        mopian:[],
        changdu:[],
        other_field2:[],
        other_field3:[],
        standard:[],
        color:[]
      }
      this.loadData(1);
    },

    viewHistory(record){
      console.log(record);
      this.$refs.purchaseHistoryModal.edit(record);
      this.$refs.purchaseHistoryModal.title = "编辑";
      this.$refs.purchaseHistoryModal.disableSubmit = false;
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
<style>
.item-img {
  cursor:pointer;
  position: static;
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
</style>